package com.app.scale.util;

import android.os.Build;
import android.os.Parcelable;

import androidx.annotation.RequiresApi;

import com.app.scale.view.UHFSilionParams;
import com.nlscan.android.uhf.TagInfo;
import com.nlscan.android.uhf.UHFCommonParams;
import com.nlscan.android.uhf.UHFManager;
import com.nlscan.android.uhf.UHFReader;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class UHFUtil {
    public static UHFManager mUHFMgr;

    public static UHFManager getInstance() {
        if(mUHFMgr == null){
            mUHFMgr = UHFManager.getInstance();
        }
        return mUHFMgr;
    }

    public static boolean getStatus() {
        if(mUHFMgr == null){
            mUHFMgr = UHFManager.getInstance();
        }
        return mUHFMgr.isPowerOn();
    }

    public static void open() {
        if(mUHFMgr == null){
            mUHFMgr = UHFManager.getInstance();
        }
        if(!mUHFMgr.isPowerOn()) {
            mUHFMgr.powerOn();
        }
    }

    public static void close() {
        if(mUHFMgr == null){
            mUHFMgr = UHFManager.getInstance();
        }
        if(mUHFMgr.isPowerOn()) {
            mUHFMgr.powerOff();
        }
    }

    public static void setMinPowerDB() {
        if(mUHFMgr == null){
            mUHFMgr = UHFManager.getInstance();
        }
        try {
            JSONArray jsItemArray = new JSONArray();
            int antportc = 1; //ant count
            for (int i = 0; i < antportc; i++) {
                int antid = i + 1;
                int readPower =  (short) 500;
                int writePower = (short) 500;
                JSONObject jsItem = new JSONObject();
                jsItem.put("antid", antid);
                jsItem.put("readPower", readPower);
                jsItem.put("writePower", writePower);
                jsItemArray.put(jsItem);
            }

            UHFReader.READER_STATE er = mUHFMgr.setParam(UHFSilionParams.RF_ANTPOWER.KEY, UHFSilionParams.RF_ANTPOWER.PARAM_RF_ANTPOWER, jsItemArray.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void setMaxPowerDB() {
        try {
            JSONArray jsItemArray = new JSONArray();
            int antportc = 1; //ant count
            for (int i = 0; i < antportc; i++) {
                int antid = i + 1;
                int readPower =  (short) 3000;
                int writePower = (short) 2700;
                JSONObject jsItem = new JSONObject();
                jsItem.put("antid", antid);
                jsItem.put("readPower", readPower);
                jsItem.put("writePower", writePower);
                jsItemArray.put(jsItem);
            }

            UHFReader.READER_STATE er = mUHFMgr.setParam(UHFSilionParams.RF_ANTPOWER.KEY, UHFSilionParams.RF_ANTPOWER.PARAM_RF_ANTPOWER, jsItemArray.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public final static String[] triggerModeItems = new String[]{"使用SCAN键触发", "侧面SCAN键触发", "枪把SCAN键触发"};
    @RequiresApi(api = Build.VERSION_CODES.N)
    public static int[] getTriggerMode() {
        List<Integer> checkedIds = new ArrayList<>();
        if(mUHFMgr == null){
            mUHFMgr = UHFManager.getInstance();
        }

        boolean mainTriggerEnable = mUHFMgr.isTriggerOn(UHFCommonParams.TRIGGER_MODE.TRIGGER_MODE_MAIN);
        boolean leftTriggerEnable = mUHFMgr.isTriggerOn(UHFCommonParams.TRIGGER_MODE.TRIGGER_MODE_LEFT);
        boolean rightTriggerEnable = mUHFMgr.isTriggerOn(UHFCommonParams.TRIGGER_MODE.TRIGGER_MODE_RIGHT);
        boolean backTriggerEnable = mUHFMgr.isTriggerOn(UHFCommonParams.TRIGGER_MODE.TRIGGER_MODE_BACK);
        if(mainTriggerEnable)checkedIds.add(0);
        if(leftTriggerEnable || rightTriggerEnable) checkedIds.add(1);
        if(backTriggerEnable)checkedIds.add(2);
        return checkedIds.stream().mapToInt(Integer::valueOf).toArray();
    }

    public static void setTriggerMode(int [] selectList) {
        if(mUHFMgr == null){
            mUHFMgr = UHFManager.getInstance();
        }
        mUHFMgr.setTrigger(UHFCommonParams.TRIGGER_MODE.TRIGGER_MODE_MAIN,false);
        mUHFMgr.setTrigger(UHFCommonParams.TRIGGER_MODE.TRIGGER_MODE_LEFT,false);
        mUHFMgr.setTrigger(UHFCommonParams.TRIGGER_MODE.TRIGGER_MODE_RIGHT,false);
        mUHFMgr.setTrigger(UHFCommonParams.TRIGGER_MODE.TRIGGER_MODE_BACK,false);

        for(int value : selectList){
            switch (value){
                case 0:
                    mUHFMgr.setTrigger(UHFCommonParams.TRIGGER_MODE.TRIGGER_MODE_MAIN,true);
                    break;
                case 1:
                    mUHFMgr.setTrigger(UHFCommonParams.TRIGGER_MODE.TRIGGER_MODE_LEFT,true);
                    mUHFMgr.setTrigger(UHFCommonParams.TRIGGER_MODE.TRIGGER_MODE_RIGHT,true);
                    break;
                case 2:
                    mUHFMgr.setTrigger(UHFCommonParams.TRIGGER_MODE.TRIGGER_MODE_BACK,true);
                    break;
            }
        }
    }

    public static void allTriggerModeOpen() {
        if(mUHFMgr == null){
            mUHFMgr = UHFManager.getInstance();
        }
//        boolean mainTriggerEnable = mUHFMgr.isTriggerOn(UHFCommonParams.TRIGGER_MODE.TRIGGER_MODE_MAIN);
//        boolean leftTriggerEnable = mUHFMgr.isTriggerOn(UHFCommonParams.TRIGGER_MODE.TRIGGER_MODE_LEFT);
//        boolean rightTriggerEnable = mUHFMgr.isTriggerOn(UHFCommonParams.TRIGGER_MODE.TRIGGER_MODE_RIGHT);
//        boolean backTriggerEnable = mUHFMgr.isTriggerOn(UHFCommonParams.TRIGGER_MODE.TRIGGER_MODE_BACK);
//        if(!mainTriggerEnable) mUHFMgr.setTrigger(UHFCommonParams.TRIGGER_MODE.TRIGGER_MODE_MAIN,true);
//        if(!leftTriggerEnable){
//            mUHFMgr.setTrigger(UHFCommonParams.TRIGGER_MODE.TRIGGER_MODE_LEFT,true);
//            mUHFMgr.setTrigger(UHFCommonParams.TRIGGER_MODE.TRIGGER_MODE_RIGHT,true);
//        }
//        if(!backTriggerEnable)    mUHFMgr.setTrigger(UHFCommonParams.TRIGGER_MODE.TRIGGER_MODE_BACK,true);
        mUHFMgr.setTrigger(UHFCommonParams.TRIGGER_MODE.TRIGGER_MODE_BACK,true);
    }



    public static String[] Coname = new String[] { "序号", "EPC ID", "次数", "天线", "协议", "RSSI", "频率", "附加数据" };//表头

    public static String getEPCId(Parcelable[] tagInfos){
        Map<String, TagInfo> TagsMap = new LinkedHashMap<String, TagInfo>();// 有序
        String epcstr = "";
        if(tagInfos != null && tagInfos.length > 0) {

            for(int i =0 ;i < tagInfos.length; i++) {
                TagInfo tag = (TagInfo) tagInfos[i];
                String epcId = HexUtil.bytesToHexString(tag.EpcId);
                if (!TagsMap.containsKey(epcId)) {
                    TagsMap.put(epcId, tag);

                    // show
                    Map<String, String> m = new HashMap<String, String>();
//                    listBean.getList().add(new BaseListData(""+Coname[0], ""+String.valueOf(TagsMap.size())));

                    epcstr =epcId;
                    if (epcstr.length() < 24)
                        epcstr = String.format("%-24s", epcstr);

//                    listBean.getList().add(new BaseListData(""+Coname[1], ""+epcstr));

                    String cs = m.get("次数");
                    if (cs == null)
                        cs = "0";
                    int isc = Integer.parseInt(cs) + tag.ReadCnt;

//                    listBean.getList().add(new BaseListData(""+Coname[2], ""+String.valueOf(isc)));
//                    listBean.getList().add(new BaseListData(""+Coname[3], ""+String.valueOf(tag.AntennaID)));
//                    listBean.getList().add(new BaseListData(""+Coname[4], ""));
//                    listBean.getList().add(new BaseListData(""+Coname[5], ""+String.valueOf(tag.RSSI)));
//                    listBean.getList().add(new BaseListData(""+Coname[6], ""+String.valueOf(tag.Frequency)));
//
//                    if (tag.EmbededDatalen > 0) {
//                        String out = HexUtil.bytesToHexString(tag.EmbededData);
//                        listBean.getList().add(new BaseListData(""+Coname[7], ""+String.valueOf(out)));
//
//                    } else{
//                        listBean.getList().add(new BaseListData(""+Coname[7], ""));
//                    }
                }
            }
        }
        return epcstr;
    }

}
